<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\7\25 0025
 * Time: 11:39
 */
namespace app\api\controller;

use app\common\controller\Api;

class GoodsDeal extends Api{

    function index(){
        $kbx_url=config('kbx_url');
        $url = $kbx_url."/api/auth/login/client";
        $data['clientId'] = "5b4f0a2322437c15382e767d";
        $data['timeStamp'] = date("YmdH:i",time());
        $data['secret'] = md5("kwiBon20l8".$data['timeStamp']);
        $data = json_encode($data);
        list($return_code,$headers,$return_content) = $this->http_post_data($url, $data);
        $data1 = json_decode($return_content);

        var_dump($data1);exit();
        print_r($return_content);exit;
        $this->sendCurl($url,$data);
    }


    function http_post_data($url, $data_string) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);		//表示需要response header

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                "Content-Type: application/json; charset=utf-8",
                "Content-Length: " . strlen($data_string))
        );
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();

        //获取头部
        $header_size	= curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $headers		= substr($return_content, 0, $header_size);
        $return_content = substr($return_content,$header_size);
        //http状态码
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        return array($return_code, $headers,$return_content);
    }

    function sendCurl($url, $data = '')
    {
        $headers = array(
            "Content-type: application/json;",
            "Accept: application/json",
            "Cache-Control: no-cache",
            "Pragma: no-cache",
        );

        //curl验证成功
        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            print curl_error($ch);
        }
        curl_close($ch);
         var_dump($result);exit();
        echo $result;
    }
}